// pages/orderDetail/orderDetail.js
Page({
  data: {
    globalData: getApp().globalData,
    order: {},
    address: {},
    header: [{
      title: '交易失败',
      footer: '删除订单'
    }, {
      title: '待付款',
      footer: '立即付款'
    }, {
      title: '待发货',
      footer: '催发货'
    }, {
      title: '待收货',
      footer: '确认收货'
    }, {
      title: '交易成功',
      footer: '删除订单'
    }],
    minute: 0,
    second: 0,
    timer: null
  },

  onLoad(options) {
    this.getOrder(options.id)
  },
  onUnload() {
    clearInterval(this.data.timer)
  },

  getOrder(id) {
    let that = this
    wx.request({
      url: that.data.globalData.server + '/order/' + id,
      method: 'GET',
      success(res) {
        that.setData({
          order: res.data.data
        })
      },
      complete() {
        that.getAddress(that.data.order.addressId)
        if(that.data.order.status === 1 && parseInt((new Date(that.data.order.created).getTime() - new Date().getTime()) / 1000 / 60) + 15 > 0) {
          let timer = setInterval(function() {
            that.judgeTimeDiffer(that.data.order.created)
          }, 1000)
          that.setData({timer: timer})
        }
      }
    })
  },
  getAddress(id) {
    let that = this
    wx.request({
      url: that.data.globalData.server + '/address/' + id,
      method: 'GET',
      success(res) {
        that.setData({
          address: res.data.data
        })
      }
    })
  },
  editOrder(status) {
    let that = this
    wx.request({
      url: that.data.globalData.server + '/order',
      method: 'PUT',
      data: {
        orderId: that.data.order.orderId, 
        paymentTime: status === 2 ? new Date() : '',
        endTime: status === 4 ? new Date() : '',
        status: status
      },
      success(res) {
        wx.redirectTo({url: '/pages/order/order?status=' + status})
      }
    })
  },
  delOrder() {
    let that = this
    wx.request({
      url: that.data.globalData.server + '/order/' + that.data.order.orderId,
      method: 'DELETE',
      success(res) {
        wx.redirectTo({url: '/pages/order/order?status=0'})
      }
    })
  },
  submit() {
    let status = this.data.order.status
    if(status === 0) {
      wx.showModal({
        title: '警告',
        content: '确认删除订单吗？',
        complete: (res) => {
          if (res.confirm) {this.delOrder()}
        }
      })
    } else if(status === 1) {
      wx.showModal({
        title: '微信支付',
        content: '茶都商城\n￥' + this.data.order.paymentMoney.toFixed(2),
        complete: (res) => {
          if (res.confirm) {
            this.editOrder(2)
          }
        }
      })
    } else if(status === 2) {
      wx.showToast({title: '已催促卖家发货'})
    } else if(status === 3) {
      this.editOrder(4)
    } else if(status === 4) {
      wx.showModal({
        title: '警告',
        content: '确认删除订单吗？',
        complete: (res) => {
          if (res.confirm) {this.delOrder()}
        }
      })
    }
  },

  /**判断距离 当前时间 间隔多少分钟*/
  judgeTimeDiffer: function (startTime) {
    var startTimes = new Date(startTime);
    console.log(parseInt((startTimes.getTime() - new Date().getTime())))
    this.setData({
      minute: parseInt((startTimes.getTime() - new Date().getTime()) / 1000 / 60) + 15,
      second: parseInt((startTimes.getTime() - new Date().getTime()) / 1000 % 60)+ 60,
    })
  },
})